package com.gskj.eduservice.controller;


import com.gskj.commonutils.ResultJson;
import com.gskj.eduservice.entity.EduCourse;
import com.gskj.eduservice.entity.vo.CoursePublishVO;
import com.gskj.eduservice.entity.vo.CourseQueryVO;
import com.gskj.eduservice.entity.vo.CourseVO;
import com.gskj.eduservice.service.IEduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-16
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private IEduCourseService iEduCourseService;

    /***
     *  添加课程、课程介绍信息
     * @param courseVO
     * @return
     */
    @PostMapping("/add/course/info")
    public ResultJson addCourse(@RequestBody CourseVO courseVO) {

        // 返回 添加后的课程id，为后续的操作使用
        String cId = iEduCourseService.saveCourseInfo(courseVO);
        return ResultJson.success().data("cId", cId);
    }

    /***
     *  根据id 查询 课程基本信息
     * @param courseId
     * @return
     */
    @GetMapping("/get/courseinfo/by/{courseid}")
    public ResultJson getCourseInfoById(
            @PathVariable("courseid") String courseId) {

        // 根据id 查询 课程基本信息
        CourseVO courseVO = iEduCourseService.getCourseById(courseId);
        return ResultJson.success().data("courseVO", courseVO);
    }

    /***
     *  修改 课程信息
     * @param courseVO
     * @return
     */
    @PostMapping("/edit/courseinfo")
    public ResultJson editCourse(
            @RequestBody CourseVO courseVO) {

        iEduCourseService.updateCourseInfo(courseVO);
        return ResultJson.success();
    }

    /***
     *  根据 课程 id 查询 课程发布的所有信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/get/course/publish/by/{id}")
    public ResultJson getCoursePublishInfoById(@PathVariable("id") String courseId) {

        CoursePublishVO coursePublishVO = iEduCourseService.getCoursePublishInfoByid(courseId);
        return ResultJson.success().data("courseFinal", coursePublishVO);
    }

    /**
     * 根据 课程id将当前课程修改 状态为 已发布状态
     *
     * @return
     */
    @GetMapping("/alter/course/status/{id}")
    public ResultJson updateCourseStatus(@PathVariable("id") String courseId) {

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        // 设置课程 状态为 Normal :已发布
        eduCourse.setStatus("Normal");
        iEduCourseService.updateById(eduCourse);

        return ResultJson.success();
    }

    /***
     * 课程列表 查询 课程条件+分页查询
     * @return
     */
    @GetMapping("/get/course/list")
    public ResultJson getCourseList() {

        List<EduCourse> list = iEduCourseService.list(null);
        return ResultJson.success().data("courseList", list);
    }

    /***
     *  删除课程信息。包括 讲师、课程描述、章节、小结
     * @param courseId
     * @return
     */
    @DeleteMapping("/remove/course/by/{id}")
    public ResultJson removeCourseById(@PathVariable("id") String courseId) {

        iEduCourseService.removeCourseById(courseId);
        return ResultJson.success();
    }

    /***
     *  查询课程 带条件分页
     * @param page 页码
     * @param size 条数
     * @param queryVO 查询条件
     * @return
     */
    @PostMapping("/find/course/page/list/{page}/{size}")
    public ResultJson getCoursePageList(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            //查询条件
            @RequestBody(required = false) CourseQueryVO queryVO) {

        Map<String, Object> objectMap = iEduCourseService.queryCoursePage(queryVO, page, size);

        return ResultJson.success().data(objectMap);
    }

}

